package com.lepu.app.fun.chat.util;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.app.utils.Const;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Vibrator;

/**
 * Created by weichyang on 2015/6/1.
 */
public class SingManager {
    private static SingManager instance = null;
    private Context context = MyApplication.getInstance();
    private AudioManager audioManager;
    private Vibrator vibrator;
    private long lastTime;

    private SingManager() {
    }

    public static SingManager getInstance() {
        if (instance == null) {
            synchronized (SingManager.class) {
                if (instance == null) {
                    instance = new SingManager();
                }
            }
        }
        return instance;
    }

    public void preparResource() {
        if (audioManager == null)
            audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
        if (vibrator == null)
            vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void setVoiceReminder() {

        boolean flag = UserConfig.getConfigBoolean(MyApplication.getInstance(),
                Const.CONFIG_APP_SETTING_CHAT_AUDIO_SWITCH, false);
        if (!flag) {
            return;
        }

        try {
            long startTime = System.currentTimeMillis();
            if (startTime - lastTime < 1000)
                return;
            int modeType = audioManager.getRingerMode();
            if (modeType == 2) { //普通模式
                MediaPlayer mp = new MediaPlayer();
                try {
                    mp.setDataSource(context, RingtoneManager
                            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (null != vibrator) {
                vibrator.vibrate(new long[]{100, 500}, -1);
            }
            lastTime = startTime;
        } catch (Exception e) {
        }

    }
}